﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase.Tools;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        decimal EKKWE1 = 0;
        decimal EKKME1 = 0;
        decimal EKKAPPLY = 0;
        decimal EKKPAIED = 0;
        private void YCAEK03FP(DataSet db)
        {
            try
            {
                EKKWE1 = 0;
                EKKME1 = 0;
                if (!db.Tables.Contains("EKKOPF"))
                    return;
                DataRow dwEKK = db.Tables["EKKOPF"].Rows[0];
                string value = "";
                Dictionary<string, int> colorpos = new Dictionary<string, int>();
                int x = 1, y = 1;
                x = 5; y = 18;
                //exAgent.SetCellsPicture(5, 17, @"D:\DS40\A1.jpg"); 
                value = dwEKK["EKKAUFKRS"].ToString() + "-" + dwEKK["EKKAUFLFD"].ToString();
                exAgent.SetCellValue(x, y, value);
                x = 3; y = 30;
                exAgent.SetCellValue(x, y, date);

                //EKKZUAB1	EKKZUABPR1	EKKZUABWE1	EKKZUABME1	EKKABRKZ1	EKKZUAB2	EKKZUABPR2	EKKZUABWE2	EKKZUABME2	EKKABRKZ2	EKKZUAB3	EKKZUABPR3	EKKZUABWE3	EKKZUABME3	EKKABRKZ3	EKKZUAB4	EKKZUABPR4	EKKZUABWE4	EKKZUABME4	EKKABRKZ4	EKKZUAB5	EKKZUABPR5	EKKZUABWE5	EKKZUABME5	EKKABRKZ5
                FillInHeadAdd(dwEKK);
                int rown = FillInFPEKPOS();
                FillinSing(db,rown);
                //FillInQPPlan(2106);
                //exAgent.DeleteRows(rown, 2100);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private int FillInFPEKPOS()
        {
            int x = 1, y = 1;
            int rown = 20;
            string value = "";
            string artdes = "", colordes = "";
            var qek = from DataRow rek in dsSource.Tables["ICC_EKPOS"].AsEnumerable()
                      orderby rek["EKPAUFPOS"].ToString() ascending
                      select rek;
            foreach (DataRow dwEKPOS in qek)
            {
                decimal applied = 0;
                decimal paied = 0;
                string ordernum = Helpers.Helper.Left(dwEKPOS["EKPAUFKRS"].ToString() + "-" + dwEKPOS["EKPAUFLFD"].ToString() + "-" + dwEKPOS["EKPAUFPOS"].ToString() + "                       ", 23);
                var queryx = from DataRow row in dsSource.Tables["EKVRECH"].AsEnumerable()
                             where row["EKVFILLER"].ToString() == ordernum
                             select row;
                foreach (DataRow row in queryx)
                {
                    if (row["EKVKIR"].ToString() == "K")
                    {
                        paied += System.Convert.ToDecimal(row["EKVBETRAG"].ToString()) / 100;
                    }
                    if (row["EKVKIR"].ToString() == " ")
                    {
                        applied += System.Convert.ToDecimal(row["EKVBETRAG"].ToString()) / 100;
                    }

                }
                if (IsApplyPayment && applied == 0)
                {
                    continue;
                }
                if (rown > 21)
                {
                    exAgent.RangeCopy(21, 23, rown + 1, rown + 3);
                }
                rown++;
                DataRow dwArticle = null;
                DataRow dwColor = null;
                string exart = "";
                int pos = System.Convert.ToInt16(dwEKPOS["EKPAUFPOS"].ToString());
                string artg = dwEKPOS["EKPARTGR"].ToString();
                string art = dwEKPOS["EKPART"].ToString();
                string var = dwEKPOS["EKPVAR"].ToString();
                var = Left(var, 2);
                string structure = "";
                string composition = "";
                string color = dwEKPOS["EKPFARBE"].ToString();
                string size = dwEKPOS["EKPGROE"].ToString();
                string cref =GetGB2312Value(dwEKPOS["EKPLSGRPBEZ"].ToString().Trim());
                string um = dwEKPOS["EKPMEEH"].ToString();
                double qty = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPME1"].ToString()) * 0.01, 2);
                double tvalue = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPWE1"].ToString()) * 0.01, 2);
                double price = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPPRME"].ToString()) * 0.001, 3);
                value = dwEKPOS["EKPLTERM"].ToString();
                exAgent.SetCellValue(4, 30, value);
                var query = from rows in dsSource.Tables["FSSTAMM"].AsEnumerable()
                            where rows["FSART"].ToString() == art
                            select rows;
                switch (artg)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    case "5":
                        query = from rows in dsSource.Tables["FSSTAMM"].AsEnumerable()
                                where rows["FSART"].ToString() == art
                                & rows["FSVAR"].ToString() == var
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwArticle = rw;
                            artdes = GetGB2312Value(rw["FSABEZ"].ToString());
                        }
                        query = from rows in dsSource.Tables["FSFARBEN"].AsEnumerable()
                                where rows["FSFART"].ToString() == art
                                & rows["FSFVAR"].ToString() == var
                               & rows["FSFFARBE"].ToString() == color
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwColor = rw;
                            colordes = GetGB2312Value(rw["FSFFBEZ"].ToString());
                        }
                        exart = artdes;
                        break;
                    case "6":
                        break;
                    case "7":
                        break;
                    case "8":
                        query = from rows in dsSource.Tables["MSSTAMM"].AsEnumerable()
                                where rows["MSMAT"].ToString() == Left(art, 14)
                                & rows["MSVAR"].ToString() == var
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwArticle = rw;
                            artdes = GetGB2312Value(rw["MSBEZ"].ToString());
                        }
                        break;
                    default:
                        break;
                }
                x = rown; y = 1;
                exAgent.SetCellValue(x, y, pos);

                x = rown; y = 4;
                exAgent.SetCellValue(x, y, art.Trim());

                x = rown; y = 12;
                exAgent.SetCellValue(x, y, color.Trim() + "(" + colordes.Trim() + ")");

                //x = rown; y = 16;//成份
                //exAgent.SetCellValue(x, y, composition);
                x = rown; y = 20;//尺寸
                exAgent.SetCellValue(x, y, size);

                x = rown; y = 24;
                exAgent.SetCellValue(x, y, qty);

                x = rown; y = 27;
                exAgent.SetCellValue(x, y, um);

                x = rown; y = 29;
                exAgent.SetCellValue(x, y, price);

                x = rown; y = 32;
                exAgent.SetCellValue(x, y, tvalue);
                rown++;//填写供应商品种信息
                x = rown; y = 4;
                exAgent.SetCellValue(x, y, exart);
                x = rown; y = 15;
                exAgent.SetCellValue(x, y, "主布色:" + cref);
              
                #region===========Surcharge/Dicount=========
                FillinLineSurcharge(dwEKPOS, x, ref rown);
                #endregion
                var qMU = from DataRow rw in dsSource.Tables["ABPOS"].AsEnumerable()
                          where rw["EKPAUFKRS"].ToString() == dwEKPOS["EKPAUFKRS"].ToString()
                          && rw["EKPAUFLFD"].ToString() == dwEKPOS["EKPAUFLFD"].ToString()
                          && rw["EKPAUFPOS"].ToString() == dwEKPOS["EKPAUFPOS"].ToString()
                          select rw;
                string delstr = "";
                string aqty = "";
                string part = "";
                int d = 0;
                if (qMU.Count() == 0)
                    rown++;
                int cut = qMU.Count();
                foreach (DataRow rw in qMU)
                {
                    //delstr += "发送到单:" + rw["EKPAUFKRS"].ToString() + "-" + rw["ABPAUFLFD"].ToString();
                    aqty = System.Math.Round(System.Convert.ToDecimal(rw["ABAQTYRESERVE"].ToString()), 2).ToString();
                    //delstr = "地址:" + GetGB2312Value(rw["LSSTRASSE1"].ToString()).Trim();
                    part ="(生产批次:"+rw["ABPAUFKRS"].ToString().Trim()+"-"
                        +Helper.GetSubRollNum(rw["ABPAUFLFD"].ToString().Trim())+")"+ rw["ABPART"].ToString().Trim() + ":" + GetGB2312Value(rw["FSABEZ"].ToString().Trim());

                    //delstr += Environment.NewLine;
                    d++;
                    x = rown + d;              
                    y = 24;
                    exAgent.SetCellValue(x, y, aqty);
                    y = 4;
                    exAgent.SetCellValue(x, y, part);

                    exAgent.RangeCopy(23, 23, x+1, x+1);
                    //x = rown; y = 27;
                    //exAgent.SetCellValue(x, y, part);               
                }
                rown = rown + d;
            }
            return rown;
        }

    }
}